package sm.pzavoli.mercato;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class TickerListAdapter extends SimpleAdapter {
	private LayoutInflater mInflater;

	public TickerListAdapter(Context context,
			List<? extends Map<String, ?>> data, int resource, String[] from,
			int[] to) {
		super(context, data, resource, from, to);
		mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		try {
            if (convertView == null) 
                convertView = mInflater.inflate(R.layout.ticker_view_row, null);
			
			if (convertView == null)
				return super.getView(position, convertView, parent);
			HashMap<String, String> map = (HashMap<String, String>)getItem(position);
			String guadagno = map.get("GuadagnoPerdita");
			guadagno = guadagno.replace(',','.');
			double d = Double.parseDouble(guadagno);
			Log.i("getView","valore d="+d);
			if ( d < 0) {
				TextView t = (TextView)convertView.findViewById(R.id.guadagnoperdita);
				t.setTextColor(Color.RED);				
			}
			// Associo il menu contestuale al simbolo
			TextView tv = (TextView)convertView.findViewById(R.id.symbol);

			super.getView(position, convertView, parent);
			return convertView;
		} catch(Exception e) {
			return super.getView(position, convertView, parent);
		}
	}

	public View getViewNew(int position, View convertView, ViewGroup parent) {
		try {
            if (convertView == null) { 
                convertView = mInflater.inflate(R.layout.ticker_view_row, null);
                convertView = new LinearLayout(parent.getContext());
                // Analizzo quante colonne ha l'adapter e creo tante TextView quante sono le colonne
                for ( int i = 0; i < getCount(); i++) {
                	
                	
                }
            }
			if (convertView == null)
				return super.getView(position, convertView, parent);
			HashMap<String, String> map = (HashMap<String, String>)getItem(position);
			String guadagno = map.get("GuadagnoPerdita");
			guadagno = guadagno.replace(',','.');
			double d = Double.parseDouble(guadagno);
			Log.i("getView","valore d="+d);
			if ( d < 0) {
				TextView t = (TextView)convertView.findViewById(R.id.guadagnoperdita);
				t.setTextColor(Color.RED);				
			}
			// Associo il menu contestuale al simbolo
			TextView tv = (TextView)convertView.findViewById(R.id.symbol);

			super.getView(position, convertView, parent);
			return convertView;
		} catch(Exception e) {
			return super.getView(position, convertView, parent);
		}
	}

}
